package com.heima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.dto.ActivitiesDto;
import com.heima.dto.ActivitiesPageDto;
import com.heima.mapper.ActivitiesMapper;
import com.heima.pojo.Activities;
import com.heima.result.PageResult;
import com.heima.service.ActivityService;
import com.heima.util.RandomUtil;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.UUID;

@Service
@Slf4j
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    private ActivitiesMapper activitiesMapper;

    /**
     * 新增活动
     * @param activitiesDto
     */
    @Override
    public void insertActivity(ActivitiesDto activitiesDto) {
        Activities activities = new Activities();
        BeanUtils.copyProperties(activitiesDto,activities);
        activities.setCode(new RandomUtil().getRandomNumber());
        activities.setCreateTime(LocalDateTime.now());
        activities.setUpdateTime(LocalDateTime.now());
        activitiesMapper.insertActivity(activities);
    }

    /**
     * 活动查询
     * @param activitiesPageDto
     * @return
     */
    @Override
    public PageResult queryActivitiesList(ActivitiesPageDto activitiesPageDto) {
        PageHelper.startPage(activitiesPageDto.getPageNum(),activitiesPageDto.getPageSize());
        Page<Activities> page = activitiesMapper.queryActivitiesList(activitiesPageDto);
        PageResult pageResult = PageResult.builder()
                .total(page.getTotal())
                .rows(page.getResult())
                .code(HttpServletResponse.SC_OK)
                .msg("查询成功")
                .build();
        log.info("查询结果：{}",pageResult);
        return pageResult;
    }

    /**
     * 修改界面查询回显
     * @param id
     * @return
     */
    @Override
    public Activities getActivitiesInfo(Integer id) {
        return activitiesMapper.getActivitiesInfo(id);
    }

    /**
     * 修改活动
     * @param activities
     */
    @Override
    public void updateActivities(Activities activities) {
        activities.setUpdateTime(LocalDateTime.now());
        activitiesMapper.updateActivities(activities);
    }

    /**
     * 删除活动
     * @param id
     */
    @Override
    public void deleteActivities(Integer id) {
        activitiesMapper.deleteActivities(id);
    }
}
